package format;

import java.io.Serializable;

/**
 * This class represent the TraitFormat's outpout.
 * 
 * @author Richard Pula
 * 
 */
public class Format implements Serializable {

	private static final long serialVersionUID = 1719908256331791486L;
	
	private static final String SEPARATOR = " ";
	private static final String F 		  = "F";
	
	private static final int    defaultNoFormat    = 0;
	private static final String defaultValEntre    = "DEFAULT";
	private static final int    defaultOctetsValue = -1;
	private static final int[]  defaultOctetsTab;
	public  static final Format defaultFormat;
	
	static {
		defaultOctetsTab = new int[2];
		defaultOctetsTab[0] = defaultOctetsValue;
		defaultOctetsTab[1] = defaultOctetsValue;
		
		defaultFormat = new Format(defaultNoFormat, defaultOctetsTab , defaultValEntre);
	}

	private int noFormat;
	private int[] octetsB10;
	private String valEntree;
	
	
	public Format(int noFormat, int[] octetB10) {
		this.noFormat  = noFormat;
		this.octetsB10 = octetB10;
	}
	
	public Format(int noFormat, int[] octetB10, String valE) {
		this.noFormat  = noFormat;
		this.octetsB10 = octetB10;
		this.valEntree = valE;
	}
	
	public int getNoFormat() {
		return noFormat;
	}
	
	public String getValEntree() {
		return this.valEntree;
	}
	
	public int getOctetB10(int j) {
		if (noFormat == 1 && j == 0)
			return octetsB10[j];
		else if ((noFormat == 2 || noFormat == 3) && j < 2)
			return octetsB10[j];
		else
			return 0;
	}	
	
	public boolean equals(Object arg0) {
		if (!(arg0 instanceof Format)) {
			return false;
		}
		if (this.noFormat == ((Format) arg0).noFormat) {
			if (this.octetsB10.length == ((Format) arg0).octetsB10.length) {
				for (int i = 0; i < this.octetsB10.length; ++i) {
					if (this.octetsB10[i] != ((Format) arg0).octetsB10[i]) {
						return false;
					}
				}
				if (this.valEntree != ((Format) arg0).valEntree)
					return false;
				return true;
			}
		}
		return false;
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();

		sb.append(F).append(this.noFormat);
		
		if (this.noFormat == defaultNoFormat) {
			sb.append(SEPARATOR).append(this.valEntree);
		}
		else {
			if (this.octetsB10[0] != defaultOctetsValue) {
				sb.append(SEPARATOR).append(this.octetsB10[0]);
			}
			if (this.octetsB10[1] != defaultOctetsValue) {
				sb.append(SEPARATOR).append(this.octetsB10[1]);
			}
		}
		
		return sb.toString();
	}

	/**
	 * Converts a String into a new Format.<br>
	 * Each attribute must be separated by a space.<br>
	 * 
	 * @param input The String to convert into a Format.
	 * @return The Format corresponding to the input.
	 * @throws InvalidFormatException If the string contains wrongs values.
	 */
	public static Format getFormatByString(String input) throws InvalidFormatException {
		String[] split = input.split(SEPARATOR);
		
		/*if (split.length != 2 && split.length != 3) {
			throw new InvalidFormatException(input);
		}*/
		
		int noFormat = defaultNoFormat;
		int[] octetsB10 = new int[2];
		String valE = "";
		
		// Convert String to int noFormat
		try {
			noFormat = Integer.parseInt(split[0].substring(1)); // TODO Verification
		
			// F0
			if (noFormat == defaultNoFormat) {
				// valEntree
				for (int i=1; i<split.length; ++i) {
					valE += split[i] + " ";
				}
				
				if (!"".equals(valE)) {
					valE = valE.substring(0, valE.length()-1);
				}
			}
			else {
				// Convert String to int octetsB10
				// octetsB10[0]
				if (1 < split.length) {
					octetsB10[0] = Integer.parseInt(split[1]);
					
					// octetsB10[1]
					if (2 < split.length) {
						octetsB10[1] = Integer.parseInt(split[2]);
					}
					else {
						octetsB10[1] = defaultOctetsValue;
					}
				}
				else {
					octetsB10 = defaultOctetsTab;
				}
			}
		}
		catch (NumberFormatException e) {
			throw new InvalidFormatException(input);
		}
		
		return new Format(noFormat, octetsB10, valE);
	}
	
	public static void autoTest(final String jdtFile, final String resFile) {
		
	}
		
}
